<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item
        label="标题"
        label-width="40px"
      >
        <el-input
          v-model="queryParams.title"
          clearable
          size="small"
          style="width: 160px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="发帖人"
        label-width="60px"
      >
        <el-input
          v-model="queryParams.authorName"
          clearable
          size="small"
          style="width: 100px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发帖时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:post:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:post:edit']"
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:post:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="50" />
<!--      <el-table-column label="创建者用户id" align="center" prop="authorId" />-->
<!--      <el-table-column label="置顶状态" align="center" prop="topStatus" />-->
      <el-table-column label="帖子标题" align="center" prop="title" :show-overflow-tooltip="true"/>
      <el-table-column label="发布人" align="center" prop="authorName" width="80" :show-overflow-tooltip="true"/>
      <el-table-column label="更新时间" align="center" prop="postUpdateTime" width="150">
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="viewNumber" width="80"/>
      <el-table-column label="评论数" align="center" prop="commentNumber" width="80"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['community:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            v-hasPermi="['community:post:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-hasPermi="['community:post:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改党员论坛帖对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input v-model="form.content" type="textarea" :autosize="{ minRows: 5, maxRows: 15}" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="图片">-->
<!--          <imageUpload v-model="form.picUrls" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="是否置顶">-->
<!--          <el-radio-group v-model="form.topStatus">-->
<!--            <el-radio :label="1" value="0">是</el-radio>-->
<!--            <el-radio :label="0" value="1">否</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      :visible.sync="viewPostOpen"
      title="帖子详情"
      center
    >
      <el-row style="margin-bottom: 20px">
        <el-col :span="4">
          <div style="font-size: large; font-weight: bolder; ">标题: </div>
        </el-col>
        <el-col :span="20">
          <div>{{ form.title }}</div>
        </el-col>
      </el-row>
      <el-row style="margin-bottom: 20px">
        <el-col :span="4">
          <div style="font-size: large; font-weight: bolder; ">正文: </div>
        </el-col>
        <el-col :span="20">
<!--          <div>{{ form.content }}</div>-->
          <div class="pre-line">{{form.content}}
          </div>
        </el-col>
      </el-row>
<!--      <el-row style="margin-bottom: 20px">-->
<!--        <el-col span="4">-->
<!--          <div style="font-size: large; font-weight: bolder; ">图片: </div>-->
<!--        </el-col>-->
<!--        <el-col span="20">-->
<!--          <el-image-->
<!--            style="width: 100px; height: 100px; "-->
<!--            :src="url"-->
<!--            :preview-src-list="srcList"-->
<!--            z-index="10000"-->
<!--          />-->
<!--        </el-col>-->
<!--      </el-row>-->
      <el-row style="margin-bottom: 20px">
        <el-col :span="4">
          <div style="font-size: large; font-weight: bolder; ">评论: </div>
        </el-col>
        <el-col :span="20">
          <el-table
            :data="commentTableData"
            style="width: 100%"
            max-height="150px"
            :cell-style="{padding: '5px 0'}"
          >
            <el-table-column
              prop="syncTime"
              label="时间"
              width="130px"
            />
            <el-table-column
              prop="userId"
              label="姓名"
              width="80px"
            />
            <el-table-column
              prop="content"
              label="评论"
            />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="80">
              <template slot-scope="scope">
                <el-button
                  v-hasPermi="['community:post:remove']"
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleCommentDelete(scope.row)"
                >删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer" style="text-align: center">
        <el-button @click="viewPostOpen = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost, exportPost } from '@/api/cpc/post'
import { listComment, delComment } from '@/api/cpc/post'
import ImageUpload from '@/components/ImageUpload'
import { dateFormat } from '@/utils/dateUtils'

export default {
  name: 'Post',
  components: {
    ImageUpload,
  },
  data() {
    return {
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 党员论坛帖表格数据
      postList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      viewPostOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        authorId: null,
        authorName: null,
        postCreateTime: null,
        postUpdateTime: null,
        topStatus: null,
        deleteFlag: null,
        title: null,
        content: null,
        picUrls: null,
        commentNumber: null,
        viewNumber: null,
        communityId: null
      },
      // 表单参数
      form: {
        topStatus: 0,
      },
      commentTableData: [],
      // 表单校验
      rules: {
        topStatus: [
          { required: true, message: '置顶状态不能为空', trigger: 'blur' }
        ],
        title: [
          { required: true, message: '帖子标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '帖子内容不能为空', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询党员论坛帖列表 */
    getList() {
      this.loading = true
      listPost(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        // console.log('帖子', response.rows)
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        postId: null,
        authorId: null,
        authorName: null,
        postCreateTime: null,
        postUpdateTime: null,
        topStatus: 0,
        deleteFlag: 0,
        title: null,
        content: null,
        picUrls: null,
        commentNumber: null,
        viewNumber: null,
        communityId: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleView(row) {
      const postId = row.postId
      getPost(postId).then(response => {
        this.form = response.data
      })
      const commentQuery = {
        postId: postId
      }
      listComment(commentQuery).then(response => {
        // console.log('评论', response)
        this.commentTableData = response.data
      })
      this.viewPostOpen = true
    },
    handleCommentDelete(row) {
      const commentId = row.commentId
      this.$confirm('是否确认删除该条评论', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delComment(commentId)
      }).then(() => {
        const commentQuery = {
          postId: row.postId
        }
        listComment(commentQuery).then(response => {
          // console.log('评论', response)
          this.commentTableData = response.rows
        })
        this.msgSuccess('删除成功')
      })
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新建帖子'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const postId = row.postId || this.ids
      getPost(postId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改党员论坛帖'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          const createTime = new Date()
          if (this.form.postId != null) {
            this.form.postUpdateTime = dateFormat('YYYY-mm-dd HH:MM:SS', createTime)
            updatePost(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            this.form.postCreateTime = dateFormat('YYYY-mm-dd HH:MM:SS', createTime)
            this.form.postUpdateTime = dateFormat('YYYY-mm-dd HH:MM:SS', createTime)
            this.form.viewNumber = 0
            this.form.commentNumber = 0
            addPost(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids
      console.log('删除数据', postIds)
      this.$confirm('是否确认删除党员论坛帖编号为"' + postIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delPost(postIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有党员论坛帖数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return exportPost(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>

<style>
.pre-line{
  white-space: pre-line;
}
</style>
